package com.songcha.library_business.helper

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.songcha.library_business.R
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.ui.view.CustomButton
import com.songcha.library_common.util.ColorUtil
import com.songcha.library_common.util.DateUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.util.sp2px
import com.songcha.library_picker.pickerview.builder.TimePickerBuilder
import com.songcha.library_picker.pickerview.view.TimePickerView
import com.songcha.library_picker.wheelview.view.WheelView
import java.text.FieldPosition
import java.util.Calendar

class TimePickerDialogHelper {
    companion object{
        private var mTimePicker: TimePickerView?=null
        private var mTvSolar:TextView?=null
        private var mTvLunar:TextView?=null

        @JvmStatic
        private fun setIndicatorSelect(position: Int){
            if(mTvSolar==null || mTvLunar==null) return
            if(position==0){
                mTvSolar!!.setTextColor(Color.parseColor("#403B3B"))
                mTvLunar!!.setTextColor(Color.parseColor("#DADCE3"))
            }else{
                mTvSolar!!.setTextColor(Color.parseColor("#DADCE3"))
                mTvLunar!!.setTextColor(Color.parseColor("#403B3B"))
            }
        }

        @JvmStatic
        fun showTimePickerDialog(context: Context, showLunar:Boolean, calendar: Calendar? = null,
                                 startYear:Int? = 1901,endYear:Int? = null, title:String?=null,
                                 showHour: Boolean = false,showMinute: Boolean = false,showSecond: Boolean = false,
                                 addView:View? = null,
                                 isBgTitle:Boolean? = false,
                                 //bgBackgroundResId:Int? = null,
                                 color:Int? = null,
                                 cancelColor:Int? = null,
                                 block:(timestamp:Long,year:Int, month:Int, day:Int,hour:Int,minute:Int,second:Int,isLunar:Boolean)->Unit){

            mTimePicker = TimePickerBuilder(context){date,isLunar,v->
                val _year = DateUtil.getYear(date)
                val _month = DateUtil.getMonth(date)
                val _day = DateUtil.getDay(date)
                val _hour = DateUtil.getHour(date)
                val _minute = DateUtil.getMinute(date)
                val _second = DateUtil.getSecond(date)
                block(date.time,_year,_month,_day,_hour,_minute,_second,isLunar)
            }.isDialog(true)
                .setGravity(Gravity.CENTER)
                .setLayoutRes(R.layout.business_dialog_picker_time){ container->
                    val dialogWidth = 312.dp2px()
                    container.layoutParams = container.layoutParams.apply{
                        width = dialogWidth
                    }

                    if(addView != null){
                        val flAddView = container.findViewById<FrameLayout>(R.id.business_fl_add_view)
                        flAddView.addView(addView)
                    }

                    val tab = container.findViewById<LinearLayout>(R.id.business_dpt_tab)
                    ViewHelper.setVisibility(showLunar,tab)

                    val tvTitle = container.findViewById<TextView>(R.id.business_tv_title)
                    val llTitle = container.findViewById<LinearLayout>(R.id.business_ll_title)

                    val tvBgTitle = container.findViewById<TextView>(R.id.business_tv_bg_title)
                    val llBgTitle = container.findViewById<LinearLayout>(R.id.business_ll_bg_title)

                    if(!title.isNullOrEmpty()){
                        if(isBgTitle!!){
                            ViewHelper.setVisibility(true,llBgTitle)
                            ViewHelper.setVisibility(false,llTitle)
                            tvBgTitle.text = title
                        }else{
                            ViewHelper.setVisibility(false,llBgTitle)
                            ViewHelper.setVisibility(true,llTitle)
                            tvTitle.text = title
                        }
                    }

                    val btnCancel = container.findViewById<CustomButton>(R.id.business_btn_cancel)
                    val btnConfirm = container.findViewById<CustomButton>(R.id.business_btn_confirm)
                    mTvSolar = container.findViewById<TextView>(R.id.business_tv_solar)
                    mTvLunar = container.findViewById<TextView>(R.id.business_tv_lunar)

                    if(color != null){
                        btnConfirm.setBtnBackgroundColor(color)
                        llBgTitle.setBackgroundColor(color)
                    }

                    if(cancelColor != null){
                        btnCancel.setBtnBackgroundColor(cancelColor)
                    }

                    setIndicatorSelect(0)

                    btnCancel.setOnClickListener {
                        mTimePicker!!.dismiss()
                    }
                    btnConfirm.setOnClickListener {
                        mTimePicker!!.returnDate()
                        mTimePicker!!.dismiss()
                    }
                    mTvSolar!!.setOnClickListener{
                        setIndicatorSelect(0)
                        mTimePicker!!.setLunarCalendar(false,true)
                    }
                    mTvLunar!!.setOnClickListener{
                        setIndicatorSelect(1)
                        mTimePicker!!.setLunarCalendar(true,false)
                    }

                }
                .setItemVisibleCount(5)
                .setDividerColor(color ?: ColorUtil.getResourceColor(context,com.songcha.library_common.R.color.primary))
                .setDividerType(WheelView.DividerType.BACKGROUND)
                .setTextColorCenter(Color.WHITE)
                .setTextColorOut(Color.parseColor("#A0A0A0"))
                .setContentTextSize(14)
                .setLineSpacingMultiplier(2.5f)
                .setRangYear(startYear ?: 1901, endYear ?: DateUtil.getCurrentYear())
                .setType(booleanArrayOf(true,true,true,showHour,showMinute,showSecond))
                .build()

            if(calendar!=null)
                mTimePicker!!.setDate(calendar)

            mTimePicker!!.show()
        }
    }
}